package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.pojo.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    @PostMapping("/sendMsg")
    public Result<String> sendMsg(@RequestBody User user, HttpSession session) {


        String phone = user.getPhone();

        if (!StringUtils.isEmpty(phone)) {
            String code = "1234";

            session.setAttribute(phone, 1234);

            session.setAttribute("phone", phone);

            return Result.success("发送成功");
        }

        return Result.error("发送失败");

    }

    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session) {

        String phone = user.getPhone();

        user.setId(Long.valueOf(phone));

        if (phone != null) {

            session.setAttribute("user" ,user.getId());

            return Result.success("登陆成功");
        }

        return Result.error("登陆失败");
    }


    @PostMapping("/loginout")
    public Result loginOut(HttpSession session){

        session.removeAttribute("user");

        return Result.success("退出成功");

    }
}
